<?php

/*
---------------------------------------------------------------------
- 项目: DNS phpsea
- 版本: 1.6
- 文件名:funcPage.inc.php
- 原作者:indraw
- 整理者:indraw
- 编写日期:2006/07/21
- 简要描述:常用分页
- 运行环境:---
- 修改记录:2006/07/28,indraw,添加新的分页方法
---------------------------------------------------------------------
*/

/*
	//常用分页显示
	getPage()             //获取分页str
	getPageArray()        //获取分页arr
	getPageHtml()         //获取分页htm

*/

//-------------------------------------------------------------------
class PageMaker
{
	public $Page = 1; //当前页
	public $Count = 0; //总条数
	public $PerDiv = 10; //每段
	public $PerPage = 20; //每页
	public $Condition = array(); //条件传递
	public $Cond = array(); //分类
	
	/*
	-----------------------------------------------------------
	简要描述：构造函数
	-----------------------------------------------------------
	*/
	public function __construct()
	{
		//参数整理
		parse_str($_SERVER['argv'][0], $arr);
		if($arr)
		{
			unset($arr['page'],$arr['jumpto']);
			$aR = array();
			foreach($arr as $key => $val)
			{
				$aR[] = $key."=".$val;
			}
		}

		if($aR)
		{
			$this->Condition['argv'] = @join("&",$aR);
		}
	}

	/*
	-----------------------------------------------------------
	函数名称：getUrl()
	简要描述：当前url
	输入：int
	修改日志：------
	-----------------------------------------------------------
	*/
	private function getUrl($page)
	{
		unset($this->Condition['argv']);
		$this->Condition['page'] = "page=".($page ? $page : 1);
		$uri = preg_replace("/&page=([0-9]+)/", "", $_SERVER['REQUEST_URI']);
		$uri = preg_replace("/page=([0-9]+)/", "", $uri);

		if(is_array($this->Condition))
		{
			if(substr($uri, -1, 1) != '?')
			{
				$uri = is_numeric(strpos($uri, "?")) ? $uri."&" : $uri."?";
			}
			return $uri.join("&", $this->Condition);
		}
		else
		{
			return $uri;
		}
	}

	/*
	-----------------------------------------------------------
	函数名称：getBegin()
	简要描述：开始页
	输入：int
	修改日志：------
	-----------------------------------------------------------
	*/
	function getBegin()
	{
		if($this->Page < 1)
		{
			$this->Page = 1;
		}
		return $this->PerPage * ($this->Page - 1);
	}

	/*
	-----------------------------------------------------------
	函数名称：getPage
	简要描述：分页处理函数
	输入：string
	输出：string
	修改日志：------
	-----------------------------------------------------------
	*/
	//  分段的页数
	//$offset  每页显示条数
	function getPage()
	{
		$totalnum = ceil($this->Count / $this->PerPage);
		if(!$this->Page)
		{
			$this->Page = 1;
		}
		if($this->Page > $this->PerDiv)
		{
			if($this->Page == 1)
			{
				$htmPage .= "\n";
			}
			else
			{
				$prevp = $this->Page - 1;
				$htmPage .= '<a href="'. $this->getUrl(1) .'" title="首页">[1]</a>';
			}
		}
		$term = $this->PerDiv;
		$f = 1;
		$l = $term;
		while($f <= $totalnum)
		{
			if(($f <= $this->Page) && ($this->Page <= $l))
			{
				$prevp = $f - 1;
				if($prevp > 0)
				{
					$htmPage .= '<a href="'. $this->getUrl($prevp) .'" title="前'.$this->PerDiv.'页">[Prev]</a>-';
				}
				else
				{
					$htmPage .= "\n";
				}
				if($l <= $totalnum)
				{
					for($page = $f; $page <= $l; $page++)
					{
						if($page == $this->Page)
						{
							$htmPage .= '[<font color="red" title="当前页">'.$page.'</font>]';
						}
						else
						{
							$htmPage .= '<a href="'. $this->getUrl($page) .'" title="'.$page.'页">['.$page.']</a>';
						}
					}
				}
				else
				{
					for($page = $f; $page <= $totalnum; $page++)
					{
						if($page == $this->Page)
						{
							$htmPage .= '[<font color="red" title="当前页">'.$page.'</font>]';
						}
						else
						{
							$htmPage .= '<a href="'. $this->getUrl($page) .'" title="'.$page.'页">['.$page.']</a>';
						}
					}
				}
				$nextp = $l + 1;
				if($nextp <= $totalnum)
				{
					$htmPage .= '-<a href="'. $this->getUrl($nextp).'" title="后'.$this->PerDiv.'页">[Next]</a>';
				}
				else
				{
					$htmPage .= "";
				}
			}
			$f = $f + $term;
			$l = $l + $term;
		}
		if($nextp <= $totalnum)
		{
			$htmPage .= '<a href="'. $this->getUrl($totalnum).'" title="尾页">['.$totalnum.']</a>';
		}
		return $htmPage;
	}

	/*
	-----------------------------------------------------------
	函数名称：getUserStyle()
	简要描述：个人后台分页
	样式：上一页 4 5 6 7 8 9 ... 下一页 共100页 到第页
	修改日志：2014/9/2 by Robot
	-----------------------------------------------------------
	*/
	function getUserStyle()
	{
		//参数整理
		parse_str($_SERVER['argv'][0], $arr);
		if($arr['jumpto'])
		{
			$this->Page = intval($arr['jumpto']);
		}

		$currpage = intval($this->Page);
		$count = $this->Count;
		$perpage = $this->PerPage;
		$max = ceil($this->Count / $this->PerPage);

		//只有1页，不显示分页
		if($max == 1)
		{
			return;
		}

		$start = max($currpage - 2, 1);
		$end = min($currpage + 4, $max);
		$start = $end == $max ? max($end - 6, 1) : $start;
		
		$tmp = "";
		if($currpage == 2)
		{
			$tmp .= '<a href="' . $this->getUrl(1) . '">上一页</a>';
		}
		else
		{
			$tmp .= '<a href="' . $this->getUrl(max($currpage - 1, 1)) . '" class="page-prev">上一页</a>';
		}
		
		for($i = $start; $i <= $end; $i++)
		{
			if($i == $currpage)
			{
				$tmp .= '<span class="page-cur">' . $i . '</span>';
			}
			else
			{
				$tmp .= '<a href="' . $this->getUrl($i) . '">[' . $i . ']</a>';
			}
		}

		//最后3页，不显示...
		if($max - $currpage > 3)
		{
			$tmp .= '<span class="page-break">...</span>';
		}
		
		$tmp .= '<a href="' . $this->getUrl(min($currpage + 1, $max)) . '">下一页</a>';

		$tmp .= '<span class="page-skip"><form action="" method="get" name="filterPageForm" id="filterPageForm">共'.$max.'页 到第<input value="' . min($currpage + 1, $max) . '" size="2" id="jumpto" name="jumpto" title="指定页码" type="text">页<button type="submit" title="指定页码">确定</button></form></span>';

		return $tmp;
	}

	/*
	-----------------------------------------------------------
	函数名称：getHtmlStyle()
	简要描述：
	输入：string
	输出：string
	修改日志：------
	-----------------------------------------------------------
	*/
	function getHtmlStyle()
	{
		if($this->Count == 0)
		{
			return;
		}
		
		//链接前缀
		$prefix = "/".$this->Cond['cate']."/";
		if($this->Cond['place']) $prefix .= $this->Cond['place']."/";
		if($this->Cond['brand']) $prefix .= $this->Cond['brand']."/";
		if($this->Cond['general']) $prefix .= $this->Cond['general']."/";
		if($this->Cond['unique']) $prefix .= $this->Cond['unique']."/";
		
		$page = intval($this->Page);
		if($page < 1)
			$page = 1;
		$maxpage = ceil($this->Count / $this->PerPage);
		if($maxpage == 0)
			$maxpage = 1;
		if($page > $maxpage)
			$page = $maxpage;
		
		//只有1页，不显示分页
		if($maxpage == 1)
		{
			return;
		}
		##########################################
		$startpage = $page > 7 ? $page - 3 : $page;
		if($startpage + 8 > $maxpage)
			$startpage = $maxpage - 8;
		$endpage = $maxpage > 11 ? ($page > 7 ? $page + 5 : 11) : $maxpage;
		if($endpage > $maxpage)
			$endpage = $maxpage;
		if($startpage >= $endpage)
			$startpage = $endpage;
		
		##########################################
		$sPageHtml = "";
		
		if($page > 1)
		{
			if($page != 2)
			{
				$sPageHtml .= '<a href="' . $prefix . 'p' . ($page - 1) . '/">上一页</a>';
			}
			else
			{
				$sPageHtml .= '<a href="' . $prefix . '">上一页</a>';
			}
		}
		if($maxpage > 11 && $page > 7)
		{
			$sPageHtml .= '<a href="' . $prefix . '">[1]</a>';
			$sPageHtml .= '<a href="' . $prefix . 'p2/">[2]</a>';
			$sPageHtml .= '<a href="' . $prefix . 'p3/">[3]</a>';
			$sPageHtml .= '<a>...</a>';
			for($i = $startpage; $i <= $endpage; $i++)
			{
				if($i == $page)
				{
					$sPageHtml .= '<b>' . $i . '</b>';
				}
				else
				{
					$sPageHtml .= '<a href="' . $prefix . 'p' . $i . '/">[' . $i . ']</a>';
				}
			}
			$sPageHtml .= '';
		}
		else
		{
			for($i = 1; $i <= $endpage; $i++)
			{
				if($i == $page)
				{
					$sPageHtml .= '<b>' . $i . '</b>';
				}
				else
				{
					if($i == 1)
					{
						$sPageHtml .= '<a href="' . $prefix . '">[1]</a>';
					}
					else
					{
						$sPageHtml .= '<a href="' . $prefix . 'p' . $i . '/">[' . $i . ']</a>';
					}
				}
			}
		}
		if($page < $endpage)
		{
			$sPageHtml .= '<a href="' . $prefix . 'p' . ($page + 1) . '/">下一页</a>';
		}
		
		return $sPageHtml;
	}
	
	/*
	-----------------------------------------------------------
	函数名称：getPageHtml()
	简要描述：分页处理函数-通过post传递数据
	输入：string
	输出：string
	修改日志：2005/6/28 by indraw
	-----------------------------------------------------------
	*/
	function getSpaceStyle()
	{
		$currpage = intval($this->Page);
		$count = $this->Count;
		$perpage = $this->PerPage;
		$max = ceil($this->Count / $this->PerPage);
		
		$start = max($currpage - $perpage, 1);
		$end = min($start + 10, $max);
		$start = $end == $max ? max($end - 10, 1) : $start;
		
		unset($tmp);
		if($currpage == 2)
		{
			$tmp .= '<a href="' . $this->Condition . '-' . '1.html">上一页</a>';
		}
		else
		{
			$tmp .= '<a href="' . $this->Condition . '-' . max($currpage - 1, 1) . '.html">上一页</a>';
		}
		
		for($i = $start; $i <= $end; $i++)
		{
			if($i == $currpage)
			{
				$tmp .= '<span style="font-weight:bold;color:#f00;font-size:15px;margin-right:8px">' . $i . '</span>';
			}
			else
			{
				if($i == 1)
				{
					$tmp .= '<a href="' . $this->Condition . '-' . $i . '.html">[' . $i . ']</a>';
				}
				else
				{
					$tmp .= '<a href="' . $this->Condition . '-' . $i . '.html">[' . $i . ']</a>';
				}
			}
		}
		
		$tmp .= '<a href="' . $this->Condition . '-' . min($currpage + 1, $max) . '.html">下一页</a>';
		
		return $tmp;
	}

}
//-----------------------------------------------------------------------------
//end
?>